package com.xmut.bs.handaccount.util;

import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;

/**
 * 检查权限的工具类
 * <p/>
 * Created by wangchenlong on 16/1/26.
 */
public class PermissionsChecker {
    private final Context mContext;

    public PermissionsChecker(Context context) {
        mContext = context.getApplicationContext();
    }

    /**
     * 返回缺少的权限集合
     */
    public String[] getLacksPermissions(String... permissions) {
        String str = "";
        for (String permission : permissions) {
            if (lacksPermission(permission)) {
                str = str.concat(permission).concat(",");
            }
        }
        if (!str.equals(""))
            str = str.substring(0, str.length() - 1);
        return str.split(",");
    }

    /**
     * 判断权限集合
     * @return true 缺少 false 已授权
     */
    public boolean lacksPermissions(String... permissions) {
        for (String permission : permissions) {
            if (lacksPermission(permission)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否缺少权限
     * @return true 缺少 false 已授权
     */
    private boolean lacksPermission(String permission) {
        return ContextCompat.checkSelfPermission(mContext, permission) ==
                PackageManager.PERMISSION_DENIED;
    }
}